'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceOutsideCtrl
 * @description
 * # AttendanceAttendanceOutsideCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AttendanceAttendanceOutsideCtrl', function (DataService, localStorageService, openApi, MapService, Util) {
        var vm = this;
        // 获取考勤详情
        var checkinfo = {};
        checkinfo = localStorageService.get("checkinfo");
        vm.param = Util.getLocalInfo("attParam");
        vm.type = "外勤";
        this.submit = function () {
            if (!vm.type) {
                Util.Toast("请选择外勤类型")
                return
            }

            // 获取当前位置信息
            MapService.getcurPositon().then(function (data) {
                var curPoint = data;
                var attPoint = {};
                attPoint.latitude = checkinfo.lat;
                attPoint.longitude = checkinfo.lng;
                vm.param.distance = MapService.getDistance(attPoint, curPoint);
                if (checkinfo.clockWithSelfie == 1 && !vm.param.attachment) {
                    Util.Toast("请在自拍后再打卡");
                    return;
                }

                // 重新获取地址
                vm.param.lat = curPoint.latitude;
                vm.param.lng = curPoint.longitude;
                vm.param.address = curPoint.address + curPoint.describe;
                vm.param.remark = "[" + vm.type + "]" + (vm.content ? vm.content : "")
                DataService.getData(openApi.punchClock, vm.param).then(function (data) {
                    if (data.code == 1) {
                        navigator.notification.beep(1);
                        Util.Toast("考勤成功", {
                            callback: Util.goBack
                        });
                        Util.clearLocalInfo("attParam")
                    } else {
                        Util.Toast(data.result);
                    }
                })
            })
        };

    });
